<template>
  <div class="closing">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="isClosingDialog=true">{{ $t('market.filter') }}</el-button>
      <el-button type="primary" size="mini" @click="balance(1)">
        {{ $t('market.cunHuoAccount') }}<!--存货关账-->
      </el-button>
      <el-button type="primary" size="mini" @click="balance(2)">
        {{ $t('market.cunHuoNoAccount') }}<!--存货反关账-->
      </el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.period') }}</span>
        <el-input v-model="period" size="mini" disabled />
      </div>
      <!--      当前关账期间-->
      <dataRow ref="mainData" />
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        get-row-keys="uuid"
        :expand-row-keys="expandKeys"
        serial
        sortable
        :tree-props="{children: 'list', hasChildren: 'hasChild'}"
      >
        <template v-slot:billSlot="col">
          <el-link v-if="col.scope.row.materialnumber" type="primary" @click="jumpMateriel1(col.scope.row.materialnumber)">
            {{ col.scope.row.materialnumber }}
          </el-link>
          <span v-else>{{ col.scope.row.stockName }}</span>
        </template>
      </jc-table>
      <!--    分页器-->
      <div class="footer">
        <jc-pagination
          :total="subDetails.total"
          :page.sync="subDetails.pageNum"
          :limit.sync="subDetails.pageSize"
          @pagination="handleGetUntreated"
        />
      </div>
    </div>
    <!-- 选择时间 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.checkPeriod')"
      model
      :visible.sync="isClosingDialog"
      :close-on-click-modal="false"
      width="600px"
      top="5vh"
      center
    >
      <div>
        <el-form :model="subDetails" size="mini" label-width="100px">
          <el-form-item :label="$t('market.startPeriod')">
            <el-date-picker
              v-model="subDetails.start"
              type="month"
              value-format="yyyy-MM"
              style="width: 152px"
              placeholder="选择期间"
            />
          </el-form-item>
          <el-form-item :label="$t('market.endPeriod')">
            <el-date-picker
              v-model="subDetails.end"
              type="month"
              value-format="yyyy-MM"
              style="width: 152px"
              placeholder="选择期间"
            />
          </el-form-item>
          <el-form-item :label="$t('market.frombillNunber')">
            <el-input v-model.trim="subDetails.frombillNo" />
          </el-form-item>
          <el-form-item :label="$t('market.fmaterial')">
            <el-input v-model.trim="subDetails.materialNumber" />
          </el-form-item>
          <el-form-item :label="$t('market.stockname')">
            <el-input v-model.trim="subDetails.stockName" />
          </el-form-item>
          <el-form-item :label="$t('market.tier')">
            <el-select v-model="subDetails.tier">
              <el-option label="1级" value="1" />
              <el-option label="2级" value="2" />
              <el-option label="3级" value="3" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isClosingDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="initialCon">{{ $t('btn.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  antibalance,
  balance,
  queryIssueDetailList
} from '@/api/financial/warehousing'
import dataRow from '@/views/financial/company/components/dataRow'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'

export default {
  name: 'InventoryDetail',
  components: {
    dataRow
  },
  mixins: [searData],
  props: {
    dataSub: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      subDetails: {
        start: '',
        end: '',
        stockName: '',
        materialNumber: '',
        frombillNo: '',
        tier: '2',
        pageNum: 1,
        pageSize: 200,
        total: 0
      },
      expandKeys: [], // 展开行
      period: '',
      loading: false,
      loading1: false,
      isClosingDialog: false,
      // 表头
      tableData: [], // 存货收发存明细表
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.materStock'), type: 'bill', prop: 'materialnumber', width: '180', align: 'left' },
        { label: this.$t('shop.fname2'), prop: 'materialname' },
        { label: this.$t('market.period'), prop: 'period' },
        { label: this.$t('market.frombillNo'), prop: 'frombillNo', width: '120' },
        { label: this.$t('market.billType'), prop: 'billtypename' },
        { label: this.$t('market.stock2'), prop: 'fstockName', width: '150' },
        { label: this.$t('market.funit'), prop: 'funitName' },
        { label: this.$t('market.inQty'), prop: 'inQty', align: 'right', width: '90' },
        { label: this.$t('market.inSingleWorth'), prop: 'inSingleWorth', align: 'right', width: '90' },
        { label: this.$t('market.inAllWorth'), prop: 'inAllWorth', align: 'right', width: '90' },
        { label: this.$t('market.outQty'), prop: 'outQty', align: 'right', width: '90' },
        { label: this.$t('market.outSingleWorth'), prop: 'outSingleWorth', align: 'right', width: '90' },
        { label: this.$t('market.outAllWorth'), prop: 'outAllWorth', align: 'right', width: '90' },
        { label: this.$t('market.resultQty'), prop: 'resultQty', align: 'right', width: '90' },
        { label: this.$t('market.resultSingleWorth'), prop: 'resultSingleWorth', align: 'right', width: '90' },
        { label: this.$t('market.resultAllWorth'), prop: 'resultAllWorth', align: 'right', width: '90' }
      ]
    }
  },
  watch: {
    dataSub: {
      handler(val) {
        // 完工入库-价格来源-跳存货收发
        if (val?.fnumber) {
          Object.assign(this.subDetails, val)
          this.subDetails.materialNumber = this.subDetails.fnumber
          this.initialCon()
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 过滤确认
    initialCon() {
      this.isClosingDialog = false
      const { start, end } = this.subDetails
      this.period = start + ' -- ' + end
      this.handleGetUntreated()
    },
    // 获取存货收发存明细表数据
    async handleGetUntreated() {
      this.loading = true
      const DATA = { ...this.subDetails }
      const RES = await queryIssueDetailList(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.list
        this.subDetails.total = RES.data.total
        const tier = this.subDetails.tier
        tier !== '1' && this.tableData.forEach(item => {
          this.expandKeys.push(item.uuid)
          if (tier === '3') {
            item.list.forEach(ite => {
              this.expandKeys.push(ite.uuid)
            })
          }
        })
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass() {
      this.currentPage = 1
      this.handleGetUntreated()
    },
    // 存货关账 存货反关账
    balance(ev) {
      if (!this.period) return
      const api = ev === 1 ? balance : antibalance
      const DATA = {
        period: this.subDetails.end
      }
      const message = ev === 1 ? '存货关账期间：' : '存货反关账期间：'
      this.$confirm(message + DATA.period, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api(DATA).then(res => {
          if (res.code === 0) {
            this.$refs.mainData.queryCompany()
            this.$message({ showClose: true, message: res.message, type: 'success' })
          } else {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
      })
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryIssueDetailList({
            pageSize: 100000,
            pageNum: 1,
            ...this.subDetails
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '存货收发明细表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
}
</style>
